import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashSet;
import java.util.Set;

@WebServlet("/demo")
public class Demo01 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        update(req,resp);
    }
    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //乱码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        InputStream inputStream = getClass().getResourceAsStream("/name.txt");
        BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
        Set<String> set =new HashSet<>();
//        while (true){
//
////            if (bufferedReader.readLine()!=null){
////                String name = bufferedReader.readLine();
////                set.add(name);
////            }else {
////                break;
////            }
//        }
        while(true){
            String name = bufferedReader.readLine();
            if(name == null){
                break;
            }
            set.add(name);
        }
//        for (String s : set) {
//            resp.getWriter().print(s);
//        }
        String s = JSON.toJSONString(set);
        resp.getWriter().print(s);
//        resp.getWriter().print(set.toString());
    }
}
